//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by mac on 16/2/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
//导入命名空间
import SnapKit

/*
    OC:     @protocol xxx<NSObject>
    Swift:  protocol xxx:NSObjectprotocol //继承自根协议
*/
//定义协议
protocol VisitorLoginViewDelegate:NSObjectProtocol{

    //声明协议方法
    //登陆的协议方法
    func userWillLogin()
    //注册的协议方法
    func userWillRegister()
}

class VisitorLoginView: UIView {
    
    //声明代理对象  注意:声明代理对象一定要用weak,防止循环引用
    weak var visitorLoginViewDelegate:VisitorLoginViewDelegate?
    
    //外界访问的重要方法
    func setupUIWithInfo(imageName:String?,tipText:String){
    
        if let img = imageName{
        
            circleIcon.image = UIImage(named: img)
                        //隐藏房子视图
            house.hidden = true
            //将circleIcon移到最顶层
            bringSubviewToFront(circleIcon)
            
        }else{
            //说明在首页
            startAnimation()
        }
        tipLabel.text = tipText

    }
    
    //圆圈的动画效果
    func startAnimation(){
    
        //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        //设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        //设置持续时间
        base.duration = 10
        //设置动画完成/页面失去活跃状态   动画不移除图层
        base.removedOnCompletion = false
        
        //开始动画
        circleIcon.layer.addAnimation(base, forKey: nil)
    }

    //自定义访客视图
    //默认的构造方法
    override init(frame: CGRect) {
        //调用父类的构造方法
        super.init(frame: frame)
        
        setupUI()
    }
    
    private func setupUI(){
    
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(house)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //添加约束对象
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        house.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(225)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    //添加点击事件的监听方法
    @objc private func loginBtnDidClick(){
        //使用代理调用协议方法
        visitorLoginViewDelegate?.userWillLogin()
    }
    @objc private func registerBtnDidClick(){
    
        visitorLoginViewDelegate?.userWillRegister()
    }

    //一旦重写了init(frame:)方法之后 系统默认该视图通过手写代码创建
    //系统就不会再让通过xib加载视图
    
    //通过xib加载的UIView及其子类会调用这个方法
    required init?(coder aDecoder: NSCoder) {
        //系统默认在这个地方执行报错
        fatalError("init(coder:) has not been implemented")
        //如果要用xib 注释上一句 打开下一句
//        super.init(coder: aDecoder) 
    }
    
    //阴影图片
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //子视图控件,使用懒加载获取 
    private lazy var circleIcon: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var house: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示用户登录的文案
//    private lazy var tipLabel: UILabel = UILabel.factoryLabel("关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    private lazy var tipLabel: UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    //登录按钮
    private lazy var loginBtn: UIButton = UIButton(backgroundImageName: "common_button_white_disable", title: "登录", titleColor: UIColor.darkGrayColor())
//    {
//    
//        let btn = UIButton()
//        
//        btn.setTitle("登录", forState: .Normal)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        return btn
//    }()
    //注册按钮
    private lazy var registerBtn: UIButton = UIButton(backgroundImageName: "common_button_white_disable", title: "注册", titleColor: themeColor)
//    {
//        
//        let btn = UIButton()
//        
//        btn.setTitle("注册", forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        return btn
//    }()
    
    
}
